package cn.wildfire.chat.kit.contact.newfriend;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wildfire.chat.app.extendsion.widgets.DispenseRecyclerView;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.contact.ContactViewModel;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.FriendRequest;

/**
 * Create by HEKE, 2019/8/15 20:23
 * TODO 处理好友添加申请界面
 */
public class FriendRequestListFragment extends Fragment {

    @BindView(R2.id.noNewFriendLinearLayout)
    LinearLayout noNewFriendLinearLayout;
    @BindView(R2.id.newFriendListLinearLayout)
    LinearLayout newFriendLinearLayout;
    @BindView(R2.id.friendRequestListRecyclerView)
    DispenseRecyclerView recyclerView;

    private ContactViewModel contactViewModel;
    private FriendRequestListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contact_new_friend_fragment, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public FriendRequestListAdapter getAdapter() {
        return adapter;
    }

    DispenseRecyclerView.OnDispenseTouchSelectListener selectListener = new DispenseRecyclerView.OnDispenseTouchSelectListener() {
        @Override
        public void clieckSlide(View view, int position) {
            FriendRequestViewHolder friendRequestViewHolder = (FriendRequestViewHolder) view.getTag();
            if (friendRequestViewHolder != null) {
                if (position == 0) {
                    friendRequestViewHolder.setClickRefuse();
                }
            }
        }

        @Override
        public boolean isItemViewAllowSlide(View view) {
            FriendRequestViewHolder friendRequestViewHolder = (FriendRequestViewHolder) view.getTag();
            if (friendRequestViewHolder != null) {
                return friendRequestViewHolder.getFriendRequest().status == 0;
            }
            return false;
        }

        @Override
        public void checkClickChildView(View clickView, View rootView, int id) {
            if (id == R2.id.acceptButton && clickView.getVisibility() == View.VISIBLE) {
                clickView.performClick();
            }
        }

    };

    private void init() {
        contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class);
        UserViewModel userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        userViewModel.userInfoLiveData().observe(this, userInfos -> {
            if (adapter != null) {
                adapter.onUserInfosUpdate(userInfos);
            }
        });


        List<FriendRequest> requests = contactViewModel.getFriendRequest();
        if (requests != null && requests.size() > 0) {
            noNewFriendLinearLayout.setVisibility(View.GONE);
            newFriendLinearLayout.setVisibility(View.VISIBLE);

            adapter = new FriendRequestListAdapter(FriendRequestListFragment.this);
            recyclerView.setMaxSlideDist(getResources().getDimensionPixelSize(R.dimen.y150));
            recyclerView.setSlideSize(1);
            adapter.setFriendRequests(requests);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//            recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
            recyclerView.setAdapter(adapter);
            recyclerView.bindItemClickIds(new int[]{R2.id.acceptButton});
            recyclerView.setOnDispenseTouchSelectListener(selectListener);

        } else {
            noNewFriendLinearLayout.setVisibility(View.VISIBLE);
            newFriendLinearLayout.setVisibility(View.GONE);
        }
    }

}
